﻿using Nest;
using XYS.Core.Models;
using XYS.Core.Util;

namespace XYS.Core.Util;

/// <summary>
/// 扩展
/// </summary>
public static partial class Extention
{
    /// <summary>
    /// 批量保存到ES
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="esClient"></param>
    /// <param name="indexName"></param>
    /// <param name="list"></param>
    /// <returns></returns>
    public static async Task<ResultModel> BulkAsync<T>(this IElasticClient esClient, string indexName, List<T> list)
        where T : class
    {
        try
        {
            var createResponse = await esClient.BulkAsync(new BulkRequest(indexName)
            {
                Operations = list.Select(x => new BulkIndexOperation<T>(x) as IBulkOperation).ToList()
            });
            if (createResponse.Errors) return ResultModel.Failed(createResponse.ToJson());
            return ResultModel.Success;
        }
        catch (Exception ex) { return ResultModel.Failed(ex.ToJson()); }
    }


    /// <summary>
    /// 批量保存到ES
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="esClient"></param>
    /// <param name="indexName"></param>
    /// <param name="list"></param>
    /// <returns></returns>
    public static ResultModel Bulk<T>(this IElasticClient esClient, string indexName, List<T> list)
        where T : class
    {
        try
        {
            var createResponse = esClient.Bulk(new BulkRequest(indexName)
            {
                Operations = list.Select(x => new BulkIndexOperation<T>(x) as IBulkOperation).ToList()
            });
            if (createResponse.Errors) return ResultModel.Failed(createResponse.ToJson());
            return ResultModel.Success;
        }
        catch (Exception ex) { return ResultModel.Failed(ex.ToJson()); }
    }


    /// <summary>
    /// 保存到ES
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="esClient"></param>
    /// <param name="indexName"></param>
    /// <param name="entity"></param>
    /// <returns></returns>
    public static async Task<ResultModel> BulkAsync<T>(this IElasticClient esClient, string indexName, T entity)
        where T : class
    {
        try
        {
            var indexResponse = await esClient.IndexAsync(entity, i => i.Index(indexName).Id(Guid.NewGuid().ToString()));
            if (indexResponse.IsValid) return ResultModel.Success;
            return ResultModel.Failed(indexResponse.OriginalException.Message, 500);
        }
        catch (Exception ex) { return ResultModel.Failed(ex.ToJson()); }
    }


    /// <summary>
    /// 保存到ES
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="esClient"></param>
    /// <param name="indexName"></param>
    /// <param name="entity"></param>
    /// <returns></returns>
    public static ResultModel Bulk<T>(this IElasticClient esClient, string indexName, T entity)
        where T : class
    {
        try
        {
            var indexResponse = esClient.Index(entity, i => i.Index(indexName).Id(Guid.NewGuid().ToString()));
            if (indexResponse.IsValid) return ResultModel.Success;
            return ResultModel.Failed(indexResponse.OriginalException.Message, 500);
        }
        catch (Exception ex) { return ResultModel.Failed(ex.ToJson()); }
    }

}
